home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / utilit~1 / gemtrm12.zoo / gemterm.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-11-16  |  24.8 KB  |  1,032 lines

  1. /*********************************************************************
  2.  * GEMTERM V1.2
  3.  * 1992 by Martin F. Gergeleit
  4.  * placed in the public domain
  5.  *
  6.  * GEMTERM COMES WITH ABSOLUTELY NO WARRANTY, NOR WILL I BE LIABLE FOR ANY
  7.  * DAMAGES INCURRED FROM THE USE OF IT. USE ENTIRELY AT YOUR OWN RISK!!!
  8.  *********************************************************************/
  9.  
  10. #include <gemfast.h>
  11. #include <osbind.h>
  12. #include <mintbind.h>
  13. #include <signal.h>
  14. #include <filesys.h>
  15. #include <basepage.h>
  16. #include "vaproto.h"
  17. #include "txtwin.h"
  18. #include "gemterm.h"
  19.  
  20. #ifdef DEBUG
  21. #include <stdio.h>
  22. #endif
  23.  
  24. /* Gcc convention for accessories */
  25.  
  26. char _stack_heap[0x8800];
  27. void *_heapbase = (void *)_stack_heap;
  28. long _stksize = sizeof(_stack_heap);
  29.  
  30. #define NULL 0L
  31.  
  32. #define GEMTERMMAGIC 01
  33. #define DO_DIALOG 0x01
  34. #define DO_KEEP 0x02
  35. #define NO_KEEP 0x04
  36.  
  37. /* HIGH_POLLRATE when terminals are opend */
  38. #define HIGH_POLLRATE    1000/20
  39.  
  40. /* LOW_POLLRATE when all terminals are closed */
  41. #define LOW_POLLRATE    750
  42.  
  43. #define FILELEN 16
  44. #define TRBUFFERLEN 120
  45.  
  46. /*********************************************************************/
  47. /* EXTERNALS                             */
  48. /*********************************************************************/
  49.  
  50. extern int  gl_apid;
  51. extern char args[];
  52. extern char *my_getenv();
  53. extern OBJECT *rs_trindex[];
  54. extern short _app;
  55.  
  56. /*********************************************************************/
  57. /* GLOBAL VARIABLES                         */
  58. /*********************************************************************/
  59.  
  60. short mint;
  61.  
  62. int   gl_hchar;
  63. int   gl_wchar;
  64. int   gl_wbox;
  65. int   gl_hbox; /* system sizes */
  66.  
  67. int   menu_id ;   /* our menu id */
  68. int   my_pgrp;
  69.  
  70. int   phys_handle;   /* physical workstation handle */
  71. int   handle;     /* virtual workstation handle */
  72. int   top_window; /* handle of topped window */
  73.  
  74. int   xdesk,ydesk,hdesk,wdesk;
  75. GRECT screen;
  76.  
  77. int   msgbuff[8]; /* event message buffer */
  78. int   keycode; /* keycode returned by event-keyboard */
  79. int   mx,my;      /* mouse x and y pos. */
  80. int   butdown; /* button state tested for, UP/DOWN */
  81. int   ret;     /* dummy return variable */
  82.  
  83. int work_in[11];  /* Input to GSX parameter array */
  84. int work_out[57]; /* Output from GSX parameter array */
  85. int pxyarray[10]; /* input point array */
  86.  
  87. int default_font = 1;
  88. int default_height = 6;
  89. int max_font = 1;
  90. short got_font = FALSE;
  91.  
  92. short got_infofile = FALSE;
  93. static char infofile[] = "U:\\etc\\gemterm.inf";
  94.  
  95. int acc_closed = TRUE;
  96.  
  97. char  appl_name[9];
  98. int   proto_status = 0;
  99.  
  100. char  *cmd_line;
  101.  
  102. char copy_buffer[MAXCOPYLEN];
  103.  
  104. char tr_buffer[TRBUFFERLEN];
  105. int  tr_index = 0;
  106.  
  107. char  path[PATHLEN], file[FILELEN];
  108. char  args[MAXARGLEN];
  109.  
  110. char  default_cwd[PATHLEN];
  111. short default_pers = FALSE;
  112. short default_keep = FALSE;
  113. short default_conv = FALSE;
  114.  
  115. int   tosrun = -1;
  116. int   tty;
  117. short tty_used = FALSE;
  118.  
  119. txt_win  *my_win;
  120. int      maxwins;
  121.  
  122. short do_dialog = TRUE;
  123. short fromtosrun = FALSE;
  124. short tr_lock = FALSE;
  125.  
  126. get_mint_vers()
  127. {
  128. long *cookie;
  129.  
  130. #ifdef DEBUG
  131.   fprintf(stderr, "get_mint_vers starts\n");
  132. #endif
  133.  
  134.   cookie = *((long **) 0x5a0);
  135.   if (!cookie)
  136.     mint = FALSE;
  137.   else {
  138.     while (*cookie) {
  139.       if (*cookie == 0x4d694e54L) {
  140.     mint = TRUE;
  141. #ifdef DEBUG
  142.         fprintf(stderr, "get_mint_vers returns (mint = TRUE)\n");
  143. #endif
  144.     return;
  145.       }
  146.       cookie += 2;
  147.     }
  148.   }
  149.   mint = FALSE;
  150. #ifdef DEBUG
  151.   fprintf(stderr, "get_mint_vers returns (mint = FALSE)\n");
  152. #endif
  153. }
  154.  
  155. long pty_create(name)
  156. char *name;
  157. {
  158. long p;
  159. char string[] = "U:\\PIPE\\PTYA";
  160.  
  161. #ifdef DEBUG
  162.   fprintf(stderr, "pty_create starts\n");
  163. #endif
  164.  
  165.   while ((p = Fcreate(string, 0x04)) < 0)
  166.     if (++string[strlen(string) - 1] > 'z') {
  167. #ifdef DEBUG
  168.       fprintf(stderr, "pty_create returns -1\n", p);
  169. #endif
  170.       return -1;
  171.     }
  172.   strcpy(name, string);
  173.   Fcntl(p, 0, F_SETFD);
  174.  
  175. #ifdef DEBUG
  176.   fprintf(stderr, "pty_create returns %d\n", p);
  177. #endif
  178.  
  179.   return p;
  180. }
  181.  
  182. main()
  183. {
  184. int i;
  185. int event;
  186. int win_index;
  187. int done;
  188. int mstatus;
  189. int pstatus;
  190. int count;
  191. GRECT win_rect;
  192. int ms_stat;
  193. int ms_stat_old = 0;
  194. struct sgttyb default_params;
  195. struct winsize wsize;
  196. long rusage[2];
  197.  
  198. #ifdef DEBUG
  199.   fprintf(stderr, "Gemterm starts\n");
  200. #endif
  201.  
  202.   Supexec(get_mint_vers);
  203.  
  204.   if (my_getenv("GEMTERMWINS"))
  205.     maxwins = atoi(my_getenv("GEMTERMWINS"));
  206.   if (maxwins <= 0)
  207.     maxwins = 4;
  208.  
  209. #ifdef DEBUG
  210.   fprintf(stderr, "Try to create %d ptys\n", maxwins);
  211. #endif
  212.  
  213.   for (my_win = NULL; maxwins > 0; maxwins--) {
  214.     my_win = (txt_win *)malloc(maxwins * sizeof(txt_win));
  215.     if (my_win != NULL)
  216.       break;
  217.   }
  218.  
  219. #ifdef DEBUG
  220.   fprintf(stderr, "Memory for %d ptys\n", maxwins);
  221. #endif
  222.  
  223.   for (i = 0; i < maxwins; i++) {
  224.     my_win[i].status = CLOSED;
  225.     if (mint)
  226.       my_win[i].my_pty = pty_create(my_win[i].my_pty_name);
  227.   }
  228.  
  229.   if (mint) {
  230.     char *tty_name;
  231.  
  232.     Fcntl(my_win[0].my_pty, &default_params, TIOCGETP);
  233.  
  234.     if ((tosrun = Fcreate("U:\\PIPE\\TOSRUN", 0)) >= 0)
  235.       Fcntl(tosrun, 0, F_SETFD);
  236.     
  237.     if (tty_name = my_getenv("GEMTERMTTY"))
  238.       tty = Fopen(tty_name, 2);
  239.     else
  240.       tty = Fopen("U:\\DEV\\MODEM1", 2);
  241.  
  242.     if (tty < 0)
  243.       tty_used = TRUE;        /* we will not use the tty */
  244.     else      
  245.       Fcntl(tty, 0, F_SETFD);
  246.   }
  247.  
  248. #ifdef DEBUG
  249.   fprintf(stderr, "opend tty, got fd: %d \n", tty);
  250. #endif
  251.  
  252.   if (mint)
  253.     my_pgrp = Pgetpgrp();
  254.  
  255.   appl_init();
  256.  
  257.   if (!_app)
  258.     menu_id=menu_register(gl_apid,"  Gemterm");
  259.  
  260.   fix_objects();
  261.  
  262.   if (_app) {
  263.     menu_bar(rs_trindex[MENU], TRUE);
  264.     graf_mouse(0,0);
  265.   }
  266.  
  267. #ifdef DEBUG
  268.   fprintf(stderr, "Did GEM initialization\n");
  269. #endif
  270.  
  271.   wind_get(0, WF_WORKXYWH, &xdesk, &ydesk, &wdesk, &hdesk);
  272.   screen.g_x = xdesk;
  273.   screen.g_y = ydesk;
  274.   screen.g_w = wdesk;
  275.   screen.g_h = hdesk;
  276.  
  277.   count = 10000;
  278.   copy_buffer[0] = '\0';
  279.  
  280.   args[0] = 0;
  281.   args[1] = '\0';
  282.  
  283.   strcpy(appl_name, "GEMINI  ");
  284.   if (my_getenv("GEMTERMAPPL") != 0) {
  285.     strncpy(appl_name, my_getenv("GEMTERMAPPL"), 8);
  286.   }
  287.   for (i = strlen(appl_name); i < 8; i++)
  288.     appl_name[i] = ' ';
  289.  
  290.   if (my_getenv("SHELL") == 0) {
  291.     strcpy(path, "U:\\BIN\\");
  292.     strcpy(file, "SH.TTP");
  293.   }
  294.   else {
  295.     strcpy(path, my_getenv("SHELL"));
  296.     for (i = strlen(path); i >= 0; i--) {
  297.       if (path[i] == '\\')
  298.         break;
  299.     }
  300.     strcpy(file, &path[i+1]);
  301.     path[i+1] = '\0';
  302.   }    
  303.  
  304.   if (my_getenv("HOME") == 0) 
  305.     strcpy(default_cwd, "U:\\");
  306.   else
  307.     strcpy(default_cwd, my_getenv("HOME"));
  308.  
  309. #ifdef DEBUG
  310.   fprintf(stderr, "Starting GEM main loop\n");
  311. #endif
  312.  
  313.   if (_app)
  314.     get_infofile();
  315.  
  316.   while (TRUE) {
  317.     event = MU_TIMER;
  318.     while (event == MU_TIMER) {
  319.       event = evnt_multi(mint ? (MU_MESAG | MU_BUTTON | MU_KEYBD | MU_TIMER):
  320.                        MU_MESAG,
  321.          2,1,1,
  322.          0,0,0,0,0,
  323.          0,0,0,0,0,
  324.          msgbuff,(long) (acc_closed ? LOW_POLLRATE : HIGH_POLLRATE),
  325.      &mx,&my,&mstatus,&pstatus,&keycode,&ret);
  326.  
  327.         if (event & MU_TIMER) {
  328.       count += 1000/(acc_closed ? LOW_POLLRATE : HIGH_POLLRATE);
  329.           if (acc_closed) {
  330.             for (i = 0; i < maxwins; i++) {
  331.           if (my_win[i].status == TO_BE_REOPEND) {
  332.                 do_ac_reopen();
  333.         break;
  334.           }
  335.         }
  336.           }
  337.  
  338.       /* every 10 seconds ask for the Gemini font */
  339.       if (count > 10000) {
  340.         do_av_protocoll();
  341.         count = 0;
  342.       }
  343.  
  344.       if ((!tr_lock) && (tosrun >= 0)) {
  345.         while (Finstat(tosrun) > 0) {
  346.           tr_buffer[tr_index] = (char)Fgetchar(tosrun, 0);
  347.           if (tr_index == (TRBUFFERLEN-1))
  348.             tr_buffer[tr_index] = '\0';
  349.           if (tr_buffer[tr_index++] == '\0') {
  350.             tr_lock = TRUE;
  351.         do_ac_open();
  352.             tr_index = 0;
  353.           }
  354.             }
  355.           }
  356.  
  357.         
  358.       graf_mkstate(&ret, &ret, &ms_stat, &ret);
  359.       if ((ms_stat & 0x2) && (!(ms_stat_old & 0x2)))  {
  360.         wind_get(0,WF_TOP,&top_window,&ret,&ret,&ret);
  361.             win_index = find_window(top_window);
  362.             if (win_index >= 0)
  363.         for (i = 0; copy_buffer[i] != '\0'; i++)
  364.               if (Foutstat(my_win[win_index].real_pty) >= 0){
  365.              Fputchar(my_win[win_index].real_pty, copy_buffer[i], 0);
  366.               }
  367.       }
  368.       ms_stat_old = ms_stat;
  369.  
  370.           for (i = 0; i < maxwins; i++) {
  371.         txt_win *win = &my_win[i];
  372.         if (win->status != CLOSED) {
  373.           if ((win->pid != NO_PID) && (Pkill(win->pid, SIGNULL) != 0) &&
  374.               (Finstat(win->real_pty) == 0)) {
  375.         int client_pty;
  376.             win->pid = NO_PID;
  377.         if (win->keep_on_exit) {
  378.               client_pty = Fopen(win->my_pty_name, 2);
  379.           if (client_pty >= 0) {
  380.             Fwrite(client_pty, 10, "\n\015<EXIT>\n\015");
  381.             Fclose(client_pty);
  382.           }
  383.         }
  384.         else {
  385.           wind_update(TRUE);
  386.           destroy_txt_win(win);
  387.           wind_update(FALSE);
  388.         }
  389.           }
  390.           if (Finstat(win->real_pty) != 0) {
  391.             update_txt_win(win);
  392.           }
  393.         }
  394.           }
  395.     }
  396.      }
  397.  
  398.    wind_update(TRUE);
  399.    wind_get(0,WF_TOP,&top_window,&ret,&ret,&ret);
  400.    cmd_line = NULL;
  401.    
  402.    if (event & MU_MESAG)
  403.      switch (msgbuff[0]) {
  404.  
  405.      case MN_SELECTED:
  406. #ifdef DEBUG
  407.        fprintf(stderr, "Got message MN_SELECTED\n");
  408. #endif
  409.        menu_tnormal(rs_trindex[MENU], msgbuff[3], TRUE);
  410.  
  411.        if (msgbuff[4] == MQUIT) {
  412.          for (i = 0; i < maxwins; i++) {
  413.         if (my_win[i].pid != NO_PID)
  414.          Pkill(-my_win[i].pid, SIGKILL);
  415.  
  416.        if (my_win[i].status == OPEN)
  417.              close_txt_win(&my_win[i]);
  418.       }
  419.  
  420.           menu_bar(rs_trindex[MENU], TRUE);
  421.           v_clsvwk(handle);
  422.       appl_exit();
  423.       return;
  424.        }
  425.  
  426.        else if (msgbuff[4] == MOPEN) {
  427.          fromtosrun = FALSE;
  428.          goto winopen;
  429.        }
  430.  
  431.        else if (msgbuff[4] == MCLOSE) {
  432.          win_index = find_window(top_window);
  433.          goto winclose;
  434.        }
  435.  
  436.        else if (msgbuff[4] == MSAVE) {
  437.          i = 0;
  438.          goto do_save;
  439.        }
  440.  
  441.        else if (msgbuff[4] == MABOUT) {
  442.        form_alert(1,"[0][  Gemterm V1.2|M. Gergeleit 1992  ][Ok]");
  443.        }
  444.  
  445.        break;
  446.  
  447.      case AC_CLOSE:
  448. #ifdef DEBUG
  449.        fprintf(stderr, "Got message AC_CLOSE\n");
  450. #endif
  451.  
  452.        if (msgbuff[3] == menu_id) {
  453.          for (i = 0; i < maxwins; i++) {
  454.        if (my_win[i].status == OPEN) {
  455.          my_win[i].status = my_win[i].persistent ? TO_BE_REOPEND : UNSHOWN;
  456.        }
  457.      }
  458.  
  459.          if (!acc_closed) {
  460.            v_clsvwk(handle);
  461.        acc_closed = TRUE;
  462.          }
  463.  
  464.          do_av_protocoll();
  465.        }
  466.        break;
  467.  
  468.      case GEMTERM_REOPEN:
  469. #ifdef DEBUG
  470.        fprintf(stderr, "Got message GEMTERM_REOPEN\n");
  471. #endif
  472.  
  473.        if (msgbuff[1] == gl_apid) {
  474.  
  475.      open_vwork();
  476.  
  477.          for (i = 0; i < maxwins; i++) {
  478.        if (my_win[i].status==TO_BE_REOPEND) {
  479.              open_txt_win(&my_win[i]);
  480.          if (my_win[i].status==OPEN)
  481.                do_av_accwindopen(my_win[i].handle);
  482.        }
  483.      }
  484.        }
  485.        break;
  486.  
  487.      case TOS_RUN_START:
  488. #ifdef DEBUG
  489.        fprintf(stderr, "Got message TOS_RUN_OPEN\n");
  490. #endif
  491.        {
  492.        short start;
  493.        short len;
  494.        
  495.        do_dialog = FALSE;
  496.        fromtosrun = TRUE;
  497.  
  498.        len = strlen(tr_buffer);
  499.  
  500.        i = 0;
  501.        if (tr_buffer[0] == GEMTERMMAGIC) {
  502.      i = 2;
  503.      if (tr_buffer[1] & DO_DIALOG)
  504.        do_dialog = TRUE;
  505.      if (tr_buffer[1] & NO_KEEP)
  506.        default_keep = FALSE;
  507.      if (tr_buffer[1] & DO_KEEP)
  508.        default_keep = TRUE;
  509.        }
  510.           
  511.        for (start = i; i < len; i++)
  512.      if (tr_buffer[i] == ' ') {
  513.        tr_buffer[i++] = '\0';
  514.        break;
  515.      }
  516.        strcpy (default_cwd, &tr_buffer[start]);
  517.  
  518.        for (start = i; i < len; i++)
  519.      if (tr_buffer[i] == ' ') {
  520.            tr_buffer[i++] = '\0';       
  521.            break;  
  522.          }
  523.        strcpy (&args[1], &tr_buffer[i]);
  524.        args[0] = (char)strlen(&tr_buffer[i]);
  525.  
  526.        strcpy(path, &tr_buffer[start]);
  527.        for (i = strlen(path); i >= 0; i--) {
  528.        if (path[i] == '\\')
  529.           break;
  530.        }
  531.        strcpy(file, &path[i+1]);
  532.        path[i+1] = '\0';
  533.  
  534.        tr_lock = FALSE;
  535.       }
  536.       goto winopen;
  537.  
  538.  
  539.      case VA_START:
  540. #ifdef DEBUG
  541.        fprintf(stderr, "Got message VA_START\n");
  542. #endif
  543.  
  544.        cmd_line = (char *)(((long)msgbuff[3]<<16) | ((long)msgbuff[4] & 0xffff));
  545.        if ((cmd_line != NULL) && (cmd_line[0] == '\0'))
  546.          cmd_line = NULL;
  547.        fromtosrun = FALSE;
  548.        do_dialog = TRUE;
  549.        goto winopen;
  550.        
  551.      case AC_OPEN:
  552. #ifdef DEBUG
  553.        fprintf(stderr, "Got message AC_OPEN\n");
  554. #endif
  555.  
  556.        if (msgbuff[4] == menu_id) {
  557.          fromtosrun = FALSE;
  558.          do_dialog = TRUE;
  559. winopen:       
  560.      open_vwork();
  561.  
  562.      if (!mint) {
  563.        form_alert(1,"[0][MiNT is not active ][Hmm]");
  564.        break;
  565.      }
  566.  
  567.      if (get_infofile())
  568.        break;
  569.  
  570.          done = FALSE;
  571.          for (i = 0; i < maxwins; i++) {
  572.        if ((my_win[i].status == UNSHOWN) || 
  573.            (my_win[i].status == TO_BE_REOPEND)) {
  574.              open_txt_win(&my_win[i]);
  575.          if (my_win[i].status==OPEN)
  576.                do_av_accwindopen(my_win[i].handle);
  577.          done = TRUE;
  578.        }
  579.      }
  580.      if (done && (cmd_line == NULL) && (!fromtosrun))
  581.        break;
  582.  
  583.      for (win_index = 0; (my_win[win_index].status != CLOSED) && 
  584.                  (win_index < maxwins); win_index++);
  585.  
  586.          if(win_index != maxwins){
  587.        if (my_win[win_index].pid != NO_PID)
  588.              Pkill(-my_win[win_index].pid, SIGKILL);
  589.  
  590.        i = do_select_dialog(&my_win[win_index], &cmd_line);
  591.        if (i == -1)
  592.          break;
  593.          
  594. do_save:
  595.        if (i == 0) {
  596.          int savefd;
  597.          int w;
  598.          char *filename;
  599.          
  600.          if ((filename = my_getenv("GEMTERMINF")) == 0)
  601.            filename = infofile;
  602.  
  603. #ifdef DEBUG
  604.          fprintf(stderr, "opening %s\n", filename);
  605. #endif
  606.  
  607.          Fdelete(filename);
  608.          if ((savefd = Fcreate(filename, 0)) >= 0) {
  609.  
  610. #ifdef DEBUG
  611.            fprintf(stderr, "start writing %s\n", filename);
  612. #endif
  613.  
  614.            Fwrite (savefd, PATHLEN, path);
  615.            Fwrite (savefd, FILELEN, file);
  616.            Fwrite (savefd, MAXARGLEN, args);
  617.            Fwrite (savefd, PATHLEN, default_cwd);
  618.            Fwrite (savefd, sizeof(default_font), &default_font);
  619.            Fwrite (savefd, sizeof(default_height), &default_height);
  620.            Fwrite (savefd, sizeof(default_pers), &default_pers);
  621.            Fwrite (savefd, sizeof(default_keep), &default_keep);
  622.            Fwrite (savefd, sizeof(default_conv), &default_conv);
  623.            for (w = 0; w < maxwins; w++)
  624.              if ((my_win[w].status == OPEN) && 
  625.                  ((my_win[w].pid != NO_PID) || (my_win[w].real_pty == tty)))
  626.                write_txt_win(&my_win[w], savefd);
  627.     
  628.            Fclose(savefd);
  629. #ifdef DEBUG
  630.            fprintf(stderr, "writing of %s completed\n", filename);
  631. #endif
  632.          }
  633.          break;
  634.        }
  635.  
  636.        if (cmd_line != NULL)
  637.          strncpy(my_win[win_index].window_name, cmd_line, WINDOW_NAME_LEN);
  638.        else if (my_win[win_index].real_pty == tty) {
  639.          tty_used = TRUE;
  640.          strncpy(my_win[win_index].window_name,"Terminal", WINDOW_NAME_LEN);
  641.        }
  642.        else
  643.          strncpy(my_win[win_index].window_name,
  644.             my_win[win_index].my_pty_name, WINDOW_NAME_LEN);
  645.            my_win[win_index].window_name[WINDOW_NAME_LEN-1] = '\0';
  646.  
  647.            strcpy(my_win[win_index].cwdir, default_cwd);
  648.            strcpy(my_win[win_index].my_args, args);
  649.  
  650.            init_txt_win(&my_win[win_index],default_font,default_height);
  651.        if (my_win[win_index].wtext == 0)
  652.          break;
  653.  
  654.            open_txt_win(&my_win[win_index]);
  655.        if (my_win[win_index].status != OPEN) {
  656.          my_win[win_index].status = CLOSED;
  657.          break;
  658.        }
  659.  
  660.            do_av_accwindopen(my_win[win_index].handle);
  661.  
  662.        wsize.ws_row = my_win[win_index].lines;
  663.        wsize.ws_col = my_win[win_index].cols;
  664.        wsize.ws_xpixel = my_win[win_index].cols * 
  665.                          my_win[win_index].c_width;
  666.        wsize.ws_ypixel = my_win[win_index].lines * 
  667.                          my_win[win_index].c_height;
  668.        Fcntl(my_win[win_index].real_pty, &wsize, TIOCSWINSZ);
  669.  
  670.        if (cmd_line == NULL)
  671.          break;
  672.          
  673. #ifdef DEBUG
  674.            fprintf(stderr, "start forking new process\n");
  675. #endif
  676.        {
  677.        int f0, f1, f2, fm1, client_pty;
  678.        int savedrive;
  679.        char savedir[PATHLEN];
  680.  
  681.          client_pty = Fopen(my_win[win_index].my_pty_name, 2);
  682.          if (client_pty < 0) {
  683.            form_alert(1,"[0][Can not open client pty ][Hmm]");
  684.          }
  685.          else {           
  686.            f0 = Fdup(0);
  687.            Fforce(0, client_pty);
  688.            f1 = Fdup(1);
  689.            Fforce(1, client_pty);
  690.            f2 = Fdup(2);
  691.            Fforce(2, client_pty);
  692.            fm1 = Fdup(-1);
  693.            Fforce(-1, client_pty);
  694.            Fclose(client_pty);
  695.  
  696.            savedrive = Dgetdrv();
  697.            Dgetpath(savedir, savedrive+1);
  698.            Dsetdrv(my_win[win_index].cwdir[0] - 'A');
  699.            Dsetpath(&my_win[win_index].cwdir[2]);
  700.  
  701.            if ((my_win[win_index].pid = 
  702.                 Pexec(100, cmd_line, args, _base->p_env)) < 0) {
  703.          my_win[win_index].pid = NO_PID;
  704.              form_alert(1,"[0][Can not execute program ][Hmm]");
  705.            } else {
  706. #ifdef DEBUG
  707.                  fprintf(stderr, "new process created\n");
  708. #endif
  709.          Fcntl(my_win[win_index].my_pty, &default_params, TIOCSETP);
  710.              Fcntl(my_win[win_index].real_pty, &my_win[win_index].pid,
  711.                                  TIOCSPGRP);
  712.              Psetpgrp(my_win[win_index].pid, my_win[win_index].pid);
  713.              Prenice(my_win[win_index].pid, -10);
  714.            }
  715.            
  716.            Dsetdrv(savedrive);
  717.            Dsetpath(savedir);
  718.  
  719.            Fforce(0, f0);
  720.            Fclose(f0);
  721.            Fforce(1, f1);
  722.            Fclose(f1);
  723.            Fforce(2, f2);
  724.            Fclose(f2);
  725.            Fforce(-1, fm1);
  726.            Fclose(fm1);
  727.          }
  728.        }
  729.          }
  730.        }
  731. #ifdef DEBUG
  732.        fprintf(stderr, "open completed\n");
  733. #endif
  734.        break;
  735.  
  736.      case VA_PROTOSTATUS:
  737. #ifdef DEBUG
  738.        fprintf(stderr, "Got message VA_PROTOSTATUS\n");
  739. #endif
  740.        proto_status = msgbuff[3];
  741.        
  742.        if (!got_font)
  743.          do_av_askfilefont();
  744.        break;
  745.  
  746.      case VA_FILEFONT:
  747. #ifdef DEBUG
  748.        fprintf(stderr, "Got message VA_FILEFONT\n");
  749. #endif
  750.  
  751.        default_font = msgbuff[3];
  752.        default_height = msgbuff[4];
  753.        got_font = TRUE;
  754.        get_infofile();
  755.        break;
  756.  
  757.      case VA_DRAGACCWIND:
  758. #ifdef DEBUG
  759.        fprintf(stderr, "Got message VA_DRAGACCWIND\n");
  760. #endif
  761.  
  762.        cmd_line = (char *)(((long)msgbuff[6]<<16) | ((long)msgbuff[7] & 0xffff));
  763.        if (cmd_line != NULL) {
  764.          win_index = find_window(msgbuff[3]);
  765.          if (win_index >= 0)
  766.        for (i = 0; i < strlen(cmd_line); i++) {
  767.          if (my_win[win_index].backslash_conv && (cmd_line[i] == '\\'))
  768.            cmd_line[i] = '/';
  769.          if (Foutstat(my_win[win_index].real_pty) > 0)
  770.                Fputchar(my_win[win_index].real_pty, (long)cmd_line[i], 0);
  771.          }
  772.        }
  773.        break;
  774.  
  775.      case WM_REDRAW:
  776. #ifdef DEBUG
  777.        fprintf(stderr, "Got message WM_REDRAW\n");
  778. #endif
  779.  
  780.        win_index = find_window(msgbuff[3]);
  781.        if (win_index >= 0)
  782.          win_rect.g_x = msgbuff[4];
  783.          win_rect.g_y = msgbuff[5];
  784.          win_rect.g_w = msgbuff[6];
  785.          win_rect.g_h = msgbuff[7];
  786.      redraw_txt_win(&my_win[win_index], &win_rect);
  787.        break;
  788.  
  789. /*     case WM_NEWTOP:*/
  790.      case WM_TOPPED:
  791. #ifdef DEBUG
  792.        fprintf(stderr, "Got message WM_TOPPED\n");
  793. #endif
  794.  
  795.        win_index = find_window(msgbuff[3]);
  796.        if (win_index >= 0)
  797.      top_txt_win(&my_win[win_index]);
  798.        break;
  799.  
  800.      case WM_CLOSED:
  801. #ifdef DEBUG
  802.        fprintf(stderr, "Got message WM_CLOSED\n");
  803. #endif
  804.  
  805.        win_index = find_window(msgbuff[3]);
  806. winclose:
  807.        if (win_index >= 0){
  808.          do_av_accwindclosed(my_win[win_index].handle);
  809.      if (pstatus & 0x4) {    /* Control Key */
  810.        if (my_win[win_index].pid != NO_PID)
  811.          Pkill(-my_win[win_index].pid, SIGHUP);
  812.            Fcntl(my_win[win_index].real_pty, &my_pgrp, TIOCSPGRP);
  813.            destroy_txt_win(&my_win[win_index]);
  814.        if (my_win[win_index].real_pty == tty)
  815.          tty_used = FALSE;
  816.      }
  817.      else if (pstatus & 0x8) {    /* Alt Key */
  818.        if (my_win[win_index].pid != NO_PID)
  819.          Pkill(-my_win[win_index].pid, SIGKILL);
  820.            Fcntl(my_win[win_index].real_pty, &my_pgrp, TIOCSPGRP);
  821.            destroy_txt_win(&my_win[win_index]);
  822.        if (my_win[win_index].real_pty == tty)
  823.          tty_used = FALSE;
  824.      }
  825.      else
  826.            close_txt_win(&my_win[win_index]);
  827.        }
  828.        break;
  829.  
  830.      case WM_SIZED:
  831.      case WM_MOVED:
  832. #ifdef DEBUG
  833.        fprintf(stderr, "Got message WM_SIZED or WM_MOVED\n");
  834. #endif
  835.  
  836.        win_index = find_window(msgbuff[3]);
  837.        if (win_index >= 0){
  838.          size_txt_win(&my_win[win_index],
  839.             msgbuff[4],msgbuff[5],msgbuff[6],msgbuff[7]);
  840.        }
  841.        break;
  842.  
  843.      case WM_FULLED:
  844. #ifdef DEBUG
  845.        fprintf(stderr, "Got message WM_FULLED\n");
  846. #endif
  847.  
  848.        win_index = find_window(msgbuff[3]);
  849.        if (win_index >= 0){
  850.          full_txt_win(&my_win[win_index]);
  851.        }
  852.        break;
  853.  
  854.      case WM_ARROWED:
  855. #ifdef DEBUG
  856.        fprintf(stderr, "Got message WM_ARROWED\n");
  857. #endif
  858.  
  859.        win_index = find_window(msgbuff[3]);
  860.        if (win_index >= 0){
  861.          arrow_txt_win(&my_win[win_index], msgbuff[4]);
  862.        }
  863.        break;
  864.  
  865.      case WM_HSLID:
  866. #ifdef DEBUG
  867.        fprintf(stderr, "Got message WM_HSLID\n");
  868. #endif
  869.  
  870.        win_index = find_window(msgbuff[3]);
  871.        if (win_index >= 0){
  872.          hslid_txt_win(&my_win[win_index], msgbuff[4]);
  873.        }
  874.        break;
  875.  
  876.      case WM_VSLID:
  877. #ifdef DEBUG
  878.        fprintf(stderr, "Got message WM_VSLID\n");
  879. #endif
  880.  
  881.        win_index = find_window(msgbuff[3]);
  882.        if (win_index >= 0){
  883.          vslid_txt_win(&my_win[win_index], msgbuff[4]);
  884.        }
  885.        break;
  886.  
  887.      } /* switch (msgbuff[0]) */
  888.  
  889.      if (event & MU_BUTTON) {
  890. #ifdef DEBUG
  891.        fprintf(stderr, "Got button event\n");
  892. #endif
  893.  
  894.        win_index = find_window(top_window);
  895.        if (win_index >= 0)
  896.          mouse_txt_win(&my_win[win_index], mx, my, 1);
  897.      }
  898.  
  899.      if(event & MU_KEYBD){
  900. #ifdef DEBUG
  901.        fprintf(stderr, "Got keyboard event\n");
  902. #endif
  903.  
  904.        win_index = find_window(top_window);
  905.        if (win_index >= 0){
  906.          if (Foutstat(my_win[win_index].real_pty) >= 0){
  907.        Fputchar(my_win[win_index].real_pty,
  908.            (keycode & 0xff) | ((keycode & 0xff00) << 8), 1);
  909.          }
  910.        }
  911.      }
  912.  
  913.    /* find the zomies, but only if I am an application, an accessory
  914.       should not collect the processes of its main appl */
  915.    if (mint && _app)
  916.      while (Pwait3(1, &rusage[0]) > 0);
  917.  
  918.    wind_update(FALSE);
  919.  
  920.  } /* while (TRUE) */
  921. }
  922.  
  923. int find_window(txt_win_handle)
  924. int txt_win_handle;
  925. {
  926. int i;
  927.  
  928.   for (i = 0; i < maxwins; i++)
  929.     if (my_win[i].handle == txt_win_handle) {
  930. #ifdef DEBUG
  931.        fprintf(stderr, "find_window returns %d\n", i);
  932. #endif
  933.       return i;
  934.     }
  935. #ifdef DEBUG
  936.   fprintf(stderr, "find_window returns -1 (not found)\n");
  937. #endif
  938.   return -1;
  939. }
  940.  
  941. open_vwork()
  942. {
  943. int i;
  944.  
  945. #ifdef DEBUG
  946.   fprintf(stderr, "open_vwork starts\n");
  947. #endif
  948.  
  949.   if (!acc_closed) {
  950. #ifdef DEBUG
  951.     fprintf(stderr, "workstation already open\n");
  952. #endif
  953.     return;
  954.   }
  955.  
  956.   phys_handle=graf_handle(&gl_wchar,&gl_hchar,&gl_wbox,&gl_hbox);
  957.  
  958.   for(i=1;i<10;work_in[i++]=1);
  959.   work_in[0]=Getrez()+2;
  960.   work_in[10]=2;
  961.   handle=phys_handle;
  962.   v_opnvwk(work_in,&handle,work_out);
  963.  
  964.   if (vq_gdos())
  965.     max_font = _intout[10] + vst_load_fonts(handle, 0);
  966. #ifdef DEBUG
  967.   fprintf(stderr, "max_font = %d\n", max_font);
  968. #endif
  969.   vsf_interior(handle,0);
  970.   vsf_style(handle,8);
  971.   vsf_color(handle,0);
  972.   vst_alignment(handle, 0, 5, &ret, &ret);
  973.  
  974.   acc_closed = FALSE;
  975.  
  976. #ifdef DEBUG
  977.   fprintf(stderr, "open_vwork ends\n");
  978. #endif
  979. }
  980.  
  981. int get_infofile ()
  982. {
  983. int i;
  984. int savefd;
  985. int ret = FALSE;
  986. char *filename;
  987.  
  988. #ifdef DEBUG
  989.   fprintf(stderr, "get_infofile starts\n");
  990. #endif
  991.  
  992.   if (!got_infofile) {
  993.     if (!mint) {
  994.       form_alert(1,"[0][MiNT is not active ][Hmm]");
  995.       got_infofile = TRUE;
  996.       return TRUE;
  997.     }
  998.  
  999.     open_vwork();
  1000.  
  1001.     if ((filename = my_getenv("GEMTERMINF")) == 0)
  1002.       filename = infofile;
  1003.     if ((savefd = Fopen(filename, 0)) >= 0) {
  1004.       Fread (savefd, PATHLEN, path);
  1005.       Fread (savefd, FILELEN, file);
  1006.       Fread (savefd, MAXARGLEN, args);
  1007.       Fread (savefd, PATHLEN, default_cwd);
  1008.       Fread (savefd, sizeof(default_font), &default_font);
  1009.       Fread (savefd, sizeof(default_height), &default_height);
  1010.       default_font = vst_font(handle, default_font);
  1011.       default_height = vst_point(handle, default_height, &ret,&ret,&ret,&ret);
  1012.       Fread (savefd, sizeof(default_pers), &default_pers);
  1013.       Fread (savefd, sizeof(default_keep), &default_keep);
  1014.       Fread (savefd, sizeof(default_conv), &default_conv);
  1015.  
  1016.       for (i = 0; i < maxwins; i++) {
  1017.         if (my_win[i].status == CLOSED) {
  1018.           read_txt_win(&my_win[i], savefd);
  1019.         }
  1020.       }
  1021.       Fclose(savefd);
  1022.       ret = TRUE;
  1023.     }
  1024.     got_infofile = TRUE;
  1025.   }
  1026. #ifdef DEBUG
  1027.   fprintf(stderr, "get_infofile returns %d\n", ret);
  1028. #endif
  1029.  
  1030.   return ret;
  1031. }
  1032.